package com.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.common.constant.SharesConstan;
import com.common.utils.*;
import com.entity.*;
import com.mapper.ShareHolderMapper;
import com.service.*;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.vo.SharesHyVsggVo;
import com.vo.SharesZjlx;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wty
 * @since 2021-11-09
 */
@Service
public class ShareHolderServiceImpl extends ServiceImpl<ShareHolderMapper, ShareHolder> implements ShareHolderService {


    @Autowired
    private SharesHyzxService sharesHyzxService;
    @Autowired
    private CodeCjlService codeCjlService;
    @Autowired
    private SharesDetailService sharesDetailService;
    @Autowired
    private SharesUnfastenService sharesUnfastenService;
    @Autowired
    private HintSharesService hintSharesService;
    @Autowired
    private SharesNoticeService sharesNoticeService;
    @Autowired
    private SharesDirectionService sharesDirectionService   ;
    @Autowired
    private SharesYjygService sharesYjygService;
    @Autowired
    private SharesYjbgService sharesYjbgService;
    @Autowired
    private SharesGdnumService sharesGdnumService;
    @Autowired
    private SharesBgczService sharesBgczService;
    @Autowired
    private SharesSshyService sharesSshyService;
    @Autowired
    private SharesGdChangeService sharesGdChangeService ;
    @Autowired
    private SharesGnService sharesGnService;
    @Autowired
    private SharesBusinessService sharesBusinessService;
    @Autowired
    private SharesDataService sharesDataService;

    @Override
    public void init() {

        //跟涨股票数据
        sharesDataService.getZdfShares();
        //通知初始化
        SharesConstan.ZF10_MAIL_MAP.clear();
        SharesConstan.BACK_MAIL_MAP.clear();
        SharesConstan.MAVG_MAP.clear();
        SharesConstan.HIGH_MAP.clear();
        SharesConstan.YD_MAP.clear();
        //流动资金初始化
        SharesConstan.GNZJ_MAP.clear();
        SharesConstan.BKZJ_MAP.clear();
        SharesConstan.GGZJ_MAP.clear();
        //主营业务
        Wrapper<SharesBusiness> bwr=new EntityWrapper<>();
        bwr.eq("mainop_type",2);//按照行业划分
        List<SharesBusiness> sharesBusinesses = sharesBusinessService.selectList(bwr);
        SharesConstan.BUSINESS_MAP.clear();
        for (SharesBusiness sharesBusiness : sharesBusinesses) {
            String code = sharesBusiness.getCode();
            if(code.substring(2).startsWith("3")){
                List<SharesBusiness> voList = SharesConstan.BUSINESS_MAP.get(code);
                if(!CollectionUtils.isEmpty(voList)){
                    voList.add(sharesBusiness);
                }else{
                    List<SharesBusiness> toList=new ArrayList<>();
                    toList.add(sharesBusiness);
                    SharesConstan.BUSINESS_MAP.put(code,toList);
                }
            }
        }

        //业绩预告
        Wrapper<SharesYjyg> wrjg=new EntityWrapper<>();
        wrjg.orderBy("noticeDate",false);
        List<SharesYjyg> sharesYjygs = sharesYjygService.selectList(wrjg);
        SharesConstan.YJYG_MAP.clear();
        for (SharesYjyg sharesYjyg : sharesYjygs) {
            String code = sharesYjyg.getCode();
            List<SharesYjyg> voList = SharesConstan.YJYG_MAP.get(code);
            if(!CollectionUtils.isEmpty(voList)){
//                voList.add(sharesYjyg);
            }else{
                List<SharesYjyg> toList=new ArrayList<>();
                toList.add(sharesYjyg);
                SharesConstan.YJYG_MAP.put(code,toList);
            }
        }
        //业绩报告
        Wrapper<SharesYjbg> bgwr=new EntityWrapper<>();
        List<SharesYjbg> sharesYjbgs = sharesYjbgService.selectList(bgwr);
        SharesConstan.YJBG_MAP.clear();
        for (SharesYjbg sharesYjbg : sharesYjbgs) {
            String code = sharesYjbg.getCode();
            List<SharesYjbg> voList = SharesConstan.YJBG_MAP.get(code);
            if(!CollectionUtils.isEmpty(voList)){
                voList.add(sharesYjbg);
            }else{
                List<SharesYjbg> toList=new ArrayList<>();
                toList.add(sharesYjbg);
                SharesConstan.YJBG_MAP.put(code,toList);
            }
        }
        //股东数据
        Wrapper<SharesGdnum> wrgd=new EntityWrapper<>();
        List<SharesGdnum> sharesGdnums = sharesGdnumService.selectList(wrgd);
        SharesConstan.GDNUM_MAP.clear();
        for (SharesGdnum sharesGdnum : sharesGdnums) {
            String code = sharesGdnum.getCode();
            List<SharesGdnum> voList = SharesConstan.GDNUM_MAP.get(code);
            if(!CollectionUtils.isEmpty(voList)){
                voList.add(sharesGdnum);
            }else{
                List<SharesGdnum> toList=new ArrayList<>();
                toList.add(sharesGdnum);
                SharesConstan.GDNUM_MAP.put(code,toList);
            }
        }
        //并购重组
        Wrapper<SharesBgcz> bgcz=new EntityWrapper<>();
        List<SharesBgcz> sharesBgczs = sharesBgczService.selectList(bgcz);
        SharesConstan.BGCZ_MAP.clear();
        for (SharesBgcz sharesBgcz : sharesBgczs) {
            String code = sharesBgcz.getCode();
            List<SharesBgcz> voList = SharesConstan.BGCZ_MAP.get(code);
            if(!CollectionUtils.isEmpty(voList)){
                voList.add(sharesBgcz);
            }else{
                List<SharesBgcz> toList=new ArrayList<>();
                toList.add(sharesBgcz);
                SharesConstan.BGCZ_MAP.put(code,toList);
            }
        }


        //股票公告
        Wrapper<SharesNotice> gpgg=new EntityWrapper<>();
        //7天
        gpgg.gt("time",DateUtil.getYMDByDaysAgo(10)+" 00:00:00");
        List<SharesNotice> notices = sharesNoticeService.selectList(gpgg);
        SharesConstan.NOTICE_MAP.clear();
        for (SharesNotice notice : notices) {
            String code = notice.getCode();
            List<SharesNotice> voList = SharesConstan.NOTICE_MAP.get(code);
            if(!CollectionUtils.isEmpty(voList)){
                voList.add(notice);
            }else{
                List<SharesNotice> toList=new ArrayList<>();
                toList.add(notice);
                SharesConstan.NOTICE_MAP.put(code,toList);
            }
        }

        //减持增持
        Wrapper<SharesDirection> zcwr=new EntityWrapper<>();
        zcwr.gt("st_time",DateUtil.getYMDByDaysAgo(30)+" 00:00:00");
        List<SharesDirection> sharesDirections = sharesDirectionService.selectList(zcwr);
        SharesConstan.DIRECTION_MAP.clear();
        for (SharesDirection sharesDirection : sharesDirections) {
            String code = sharesDirection.getCode();
            List<SharesDirection> voList = SharesConstan.DIRECTION_MAP.get(code);
            if(!CollectionUtils.isEmpty(voList)){
                voList.add(sharesDirection);
            }else{
                List<SharesDirection> toList=new ArrayList<>();
                toList.add(sharesDirection);
                SharesConstan.DIRECTION_MAP.put(code,toList);
            }
        }


        //解禁日期
        Wrapper<SharesUnfasten> wru=new EntityWrapper<>();
        List<SharesUnfasten> unfastenList = sharesUnfastenService.selectList(wru);
        SharesConstan.ALL_UNFASTEN_MAP.clear();
        for (SharesUnfasten sharesUnfasten : unfastenList) {
            String code = sharesUnfasten.getCode();
            List<SharesUnfasten> voList = SharesConstan.ALL_UNFASTEN_MAP.get(code);
            if(!CollectionUtils.isEmpty(voList)){
                voList.add(sharesUnfasten);
            }else{
                List<SharesUnfasten> listVo=new ArrayList<>();
                listVo.add(sharesUnfasten);
                SharesConstan.ALL_UNFASTEN_MAP.put(code,listVo);
            }
        }



        //行业信息
        SharesConstan.CODE_DETAIL_MAP.clear();
        SharesConstan.CODE_LIST.clear();
        SharesConstan.GZ_SHARES_MAP.clear();
        SharesConstan.GZ_HINTSHARES_MAP.clear();
        Wrapper<SharesDetail> wr=new EntityWrapper<>();
        wr.eq("status",88);
//        wr.eq("status",SharesConstan.ALL_SHARES);
        SharesConstan.CODE_LIST= sharesDetailService.selectList(wr);
        for (SharesDetail sharesDetail : SharesConstan.CODE_LIST) {
            String code = sharesDetail.getCode();
            SharesConstan.CODE_DETAIL_MAP.put(code,sharesDetail);
            String type = sharesDetail.getType();
            if(!StringUtil.isEmpty(type)&&SharesConstan.GZ.equals(type)){
                SharesConstan.GZ_SHARES_MAP.put(code,sharesDetail);
            }
        }

        //提示股
        SharesConstan.ORDERINT=0;
        SharesConstan.ASORDERINT=0;
        Wrapper<HintShares> wrhs=new EntityWrapper<>();
        wrhs.orderBy("id");
        wrhs.gt("time",DateUtil.getYMDByDaysAgo(7));
        List<HintShares> hintShares = hintSharesService.selectList(wrhs);
        SharesConstan.ALL_INFORM_SHARES_MAP.clear();
        SharesConstan.INFORM_SHARES_MAP.clear();
        SharesConstan.HI_SHARES_MAP.clear();
        SharesConstan.HI_HINTSHARES_MAP.clear();



        for (HintShares hintShare : hintShares) {
            List<HintShares> hList = SharesConstan.ALL_INFORM_SHARES_MAP.get(hintShare.getCode());
            if(CollectionUtils.isEmpty(hList)){
                List<HintShares> vList=new ArrayList<>();
                vList.add(hintShare);
                //提示次数
                SharesConstan.ALL_INFORM_SHARES_MAP.put(hintShare.getCode(),vList);
            }else{
                hList.add(hintShare);
            }
            //今天已经提示过的股
            if(DateUtil.getDate_yyyyMMdd().equals(hintShare.getTime())){
                hintShare.setTsOrder(SharesConstan.ORDERINT++);
                SharesConstan.INFORM_SHARES_MAP.put(hintShare.getCode(),hintShare);
            }
            //最近7天涨幅大于10的股
            String newZdf = hintShare.getNewZdf();
            if(!StringUtil.isEmpty(newZdf)&&new BigDecimal(newZdf).compareTo(new BigDecimal(10))==1){
                String code = hintShare.getCode();
                SharesDetail sharesDetail = SharesConstan.CODE_DETAIL_MAP.get(code);
                if(null!=sharesDetail){
                    sharesDetail.setType(SharesConstan.HI);
                    SharesConstan.HI_SHARES_MAP.put(code,sharesDetail);
                }
            }
        }


        //获取前一天的数据
        SharesConstan.CODE_RED_LIST.clear();
        SharesConstan.CODE_MAP.clear();

        Wrapper<CodeCjl> wrz=new EntityWrapper<>();
        wrz.orderBy("id",false);
        wrz.last(" limit 1");
        List<CodeCjl> codeCjls = codeCjlService.selectList(wrz);

        if(!CollectionUtils.isEmpty(codeCjls)){
            Wrapper<CodeCjl> wrr=new EntityWrapper<>();
            wrr.eq("time",codeCjls.get(0).getTime());
            SharesConstan.CODE_RED_LIST = codeCjlService.selectList(wrr);
            for (CodeCjl codeCjl : SharesConstan.CODE_RED_LIST) {
                SharesConstan.CODE_MAP.put(codeCjl.getCode(),codeCjl);
            }
        }

        //股东信息
        SharesConstan.GD_MAP.clear();

        Wrapper<ShareHolder> wrho=new EntityWrapper<>();
        List<ShareHolder> shareHolders = this.selectList(wrho);

        for (ShareHolder shareHolder : shareHolders) {
            String code = shareHolder.getCode();
            String substring = shareHolder.getTime().substring(0, 10);
            List<ShareHolder> vos = SharesConstan.GD_MAP.get(code + substring);
            if(CollectionUtils.isEmpty(vos)){
                List<ShareHolder> vo=new ArrayList<>();
                vo.add(shareHolder);
                SharesConstan.GD_MAP.put(code + substring,vo);
            }else{
                vos.add(shareHolder);
            }
        }

        //行业资讯和行业研报

        Wrapper<SharesHyzx> hzwr=new EntityWrapper<>();
        List<SharesHyzx> sharesHyzxes = sharesHyzxService.selectList(hzwr);
        SharesConstan.HYZX_MAP.clear();
        for (SharesHyzx sharesHyzx : sharesHyzxes) {

            String sshy = sharesHyzx.getName();
            List<SharesHyzx> voList = SharesConstan.HYZX_MAP.get(sshy);
            if(!CollectionUtils.isEmpty(voList)){
                voList.add(sharesHyzx);
            }else{
                List<SharesHyzx> toList=new ArrayList<>();
                toList.add(sharesHyzx);
                SharesConstan.HYZX_MAP.put(sshy,toList);
            }
        }

        //股东数据变化
        Wrapper<SharesGdChange> cgwr=new EntityWrapper<>();
        cgwr.orderBy("id");
        List<SharesGdChange> sharesGdChanges = sharesGdChangeService.selectList(cgwr);
        SharesConstan.GDCHANGE_MAP.clear();

        for (SharesGdChange sharesGdChange : sharesGdChanges) {
            String code = sharesGdChange.getCode();
            List<SharesGdChange> voList = SharesConstan.GDCHANGE_MAP.get(code);
            if(!CollectionUtils.isEmpty(voList)){
                voList.add(sharesGdChange);
            }else{
                List<SharesGdChange> toList=new ArrayList<>();
                toList.add(sharesGdChange);
                SharesConstan.GDCHANGE_MAP.put(code,toList);
            }
        }


    }

    /**
     * 更新数据
     */
    @Override
    public void updateShares() {
        //行业资讯
        System.out.println("行业咨询抓取");
        this.gethyzx();
        //并购重组
        System.out.println("并购重组抓取");
        this.getbgcz();
        //最近质押情况
        //最近解禁情况
    }

    @Override
    public void gethyzx() {

        //删除之前的
        Wrapper<SharesHyzx> wrd=new EntityWrapper<>();
        sharesHyzxService.delete(wrd);

        Wrapper<SharesSshy> wr=new EntityWrapper<>();
        List<SharesSshy> sharesSshies = sharesSshyService.selectList(wr);

        for (SharesSshy sharesSshy : sharesSshies) {

            try{
                String name = sharesSshy.getName();
                String html = sharesSshy.getHtml();
                String url="https://stock.eastmoney.com"+html+".html";

                HttpGet get=new HttpGet(url);
                CloseableHttpResponse execute = HttpClient5Utils.httpclient.execute(get);
                String result = EntityUtils.toString(execute.getEntity(), "UTF-8");

                Document document = Jsoup.parse(result);
                Elements common_list = document.getElementsByClass("common_list");
                System.out.println(common_list.size());
                Element element = common_list.get(0);
                Elements li = element.getElementsByTag("li");

                List<SharesHyzx> sharesHyzxes=new ArrayList<>();
                for (Element element1 : li) {
                    SharesHyzx sharesHyzx=new SharesHyzx();
                    sharesHyzx.setTitle(element1.text());
                    sharesHyzx.setType("行业资讯");
                    sharesHyzx.setName(name);
                    sharesHyzxes.add(sharesHyzx);
                }

                Element elementyb = common_list.get(2);
                Elements li1 = elementyb.getElementsByTag("li");
                for (Element element1 : li1) {
                    System.out.println(element1.text());
                    SharesHyzx sharesHyzx=new SharesHyzx();
                    sharesHyzx.setTitle(element1.text());
                    sharesHyzx.setType("行业研报");
                    sharesHyzx.setName(name);
                    sharesHyzxes.add(sharesHyzx);
                }
                sharesHyzxService.insertBatch(sharesHyzxes);
            }catch (Exception e){
                e.printStackTrace();
            }

            //分词处理
            //判断是否包含股票
            //语义处理

            //异动改成最高点突破10的

        }
    }

    @Override
    public void getbgcz() {
        Wrapper<SharesBgcz> wrd=new EntityWrapper<>();
        sharesBgczService.delete(wrd);
        for (int i=1;i<30;i++){
            String url="https://datacenter-web.eastmoney.com/api/data/get" +
                    "?callback=jQuery11230429379404784511_"+new Date().getTime()+
                    "&st=SCGGRQ&sr=-1&ps=50&p=" +
                    i +
                    "&sty=ALL&source=WEB" +
                    "&token=894050c76af8597a853f5b408b759f5d" +
                    "&type=RPTA_WEB_BGCZMX";

            try {
                HttpGet get=new HttpGet(url);
                CloseableHttpResponse execute = HttpClient5Utils.httpclient.execute(get);
                String result = EntityUtils.toString(execute.getEntity(), "UTF-8");
                int rIndex = result.indexOf("(");
                String resultStr = result.substring(rIndex + 1, result.length() - 2);
                JSONObject datas2 = JSONObject.parseObject(resultStr);
                String data = datas2.getString("result");
                String klines = JSONObject.parseObject(data).getString("data");
                List<Object> parse = (List<Object>) JSON.parse(klines);

                List<SharesBgcz> sharesBgczs=new ArrayList<>();
                for (Object o : parse) {
                    SharesBgcz sharesBgcz=new SharesBgcz()  ;
                    String announdate = JSONObject.parseObject(o.toString()).getString("ANNOUNDATE");
                    String G_GOMNAME = JSONObject.parseObject(o.toString()).getString("G_GOMNAME");
                    String H_COMNAME = JSONObject.parseObject(o.toString()).getString("H_COMNAME");
                    String ZRFS = JSONObject.parseObject(o.toString()).getString("ZRFS");
                    String S_COMNAME = JSONObject.parseObject(o.toString()).getString("S_COMNAME");
                    String SCGGRQ = JSONObject.parseObject(o.toString()).getString("SCGGRQ");
                    String SCODE = JSONObject.parseObject(o.toString()).getString("SCODE");
                    String OBJTYPE = JSONObject.parseObject(o.toString()).getString("OBJTYPE");
                    String JYJE = JSONObject.parseObject(o.toString()).getString("JYJE");
                    sharesBgcz.setJyje(JYJE);
                    sharesBgcz.setCode(SCODE);
                    sharesBgcz.setObjType(OBJTYPE);
                    sharesBgcz.setNoTime(SCGGRQ);

                    if(!StringUtil.isEmpty(S_COMNAME)){
                        if(S_COMNAME.length()>50){
                            sharesBgcz.setSGomname(S_COMNAME.substring(0,50));
                        }else{
                            sharesBgcz.setSGomname(S_COMNAME);
                        }
                    }

                    if(!StringUtil.isEmpty(H_COMNAME)){
                        if(H_COMNAME.length()>50){
                            sharesBgcz.setHGomname(H_COMNAME.substring(0,50));
                        }else{
                            sharesBgcz.setHGomname(H_COMNAME);
                        }
                    }

                    if(!StringUtil.isEmpty(G_GOMNAME)){
                        if(G_GOMNAME.length()>50){
                            sharesBgcz.setGGomname(G_GOMNAME.substring(0,50));
                        }else{
                            sharesBgcz.setGGomname(G_GOMNAME);
                        }
                    }
                    sharesBgcz.setTime(announdate);
                    sharesBgcz.setType(ZRFS);
                    sharesBgczs.add(sharesBgcz);
                }
                sharesBgczService.insertBatch(sharesBgczs);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
       
    }

    /**
     * 股东数据
     */
    @Override
    public void getgdnum() {
        Wrapper<SharesGdnum> wrd=new EntityWrapper<>();
        sharesGdnumService.delete(wrd);

        List<SharesGdnum>   gdList=new ArrayList<>();
        for (int i=0;i<=92;i++){
            String url="https://datacenter-web.eastmoney.com/api/data/v1/get" +
                    "?callback=jQuery112307238029174965088_"+new Date().getTime()+
                    "&sortColumns=HOLD_NOTICE_DATE" +
                    "%2CSECURITY_CODE&sortTypes=-1%2C-1&pageSize=50" +
                    "&pageNumber=" +
                    i +
                    "&reportName=RPT_HOLDERNUMLATEST" +
                    "&columns=SECURITY_CODE%2CSECURITY_NAME_ABBR" +
                    "%2CEND_DATE%2CINTERVAL_CHRATE%2CAVG_MARKET_CAP%2CAVG_HOLD_NUM" +
                    "%2CTOTAL_MARKET_CAP%2CTOTAL_A_SHARES%2CHOLD_NOTICE_DATE%2CHOLDER_NUM" +
                    "%2CPRE_HOLDER_NUM%2CHOLDER_NUM_CHANGE%2CHOLDER_NUM_RATIO%2CEND_DATE" +
                    "%2CPRE_END_DATE&quoteColumns=f2%2Cf3&source=WEB&client=WEB";
            try {
                HttpGet get=new HttpGet(url);
                CloseableHttpResponse execute = HttpClient5Utils.httpclient.execute(get);
                String result = EntityUtils.toString(execute.getEntity(), "UTF-8");
                int rIndex = result.indexOf("(");
                String resultStr = result.substring(rIndex + 1, result.length() - 2);
                JSONObject datas2 = JSONObject.parseObject(resultStr);
                String data = datas2.getString("result");
                String klines = JSONObject.parseObject(data).getString("data");
                List<Object> parse = (List<Object>) JSON.parse(klines);

                List<SharesGdnum> sharesGdnums=new ArrayList<>();
                for (Object o : parse) {
                    SharesGdnum sharesBgcz=new SharesGdnum()  ;
                    String HOLDER_NUM = JSONObject.parseObject(o.toString()).getString("HOLDER_NUM");
                    String END_DATE = JSONObject.parseObject(o.toString()).getString("END_DATE");
                    String HOLDER_NUM_CHANGE = JSONObject.parseObject(o.toString()).getString("HOLDER_NUM_CHANGE");
                    String HOLDER_NUM_RATIO = JSONObject.parseObject(o.toString()).getString("HOLDER_NUM_RATIO");
                    String HOLD_NOTICE_DATE = JSONObject.parseObject(o.toString()).getString("HOLD_NOTICE_DATE");
                    String PRE_END_DATE = JSONObject.parseObject(o.toString()).getString("PRE_END_DATE");
                    String PRE_HOLDER_NUM = JSONObject.parseObject(o.toString()).getString("PRE_HOLDER_NUM");
                    String SECURITY_CODE = JSONObject.parseObject(o.toString()).getString("SECURITY_CODE");
                    String SECURITY_NAME_ABBR = JSONObject.parseObject(o.toString()).getString("SECURITY_NAME_ABBR");
                    String TOTAL_A_SHARES = JSONObject.parseObject(o.toString()).getString("TOTAL_A_SHARES");
                    String f2 = JSONObject.parseObject(o.toString()).getString("f2");
                    String f3 = JSONObject.parseObject(o.toString()).getString("f3");
                    String AVG_HOLD_NUM = JSONObject.parseObject(o.toString()).getString("AVG_HOLD_NUM");
                    String AVG_MARKET_CAP = JSONObject.parseObject(o.toString()).getString("AVG_MARKET_CAP");

                    sharesBgcz.setAvgHoldNum(AVG_HOLD_NUM);
                    sharesBgcz.setAvgMarketCap(AVG_MARKET_CAP);
                    sharesBgcz.setF2(f2);
                    sharesBgcz.setF3(f3);
                    sharesBgcz.setEndDate(END_DATE);
                    sharesBgcz.setTotalAShares(TOTAL_A_SHARES);
                    sharesBgcz.setName(SECURITY_NAME_ABBR);
                    sharesBgcz.setCode(SECURITY_CODE);
                    sharesBgcz.setPreEndDate(PRE_END_DATE);
                    sharesBgcz.setPreHolderNum(PRE_HOLDER_NUM   );
                    sharesBgcz.setHolderNum(HOLDER_NUM);
                    sharesBgcz.setHolderNumChange(HOLDER_NUM_CHANGE);
                    sharesBgcz.setHolderNumRatio(HOLDER_NUM_RATIO);
                    sharesBgcz.setHoldNoticeDate(HOLD_NOTICE_DATE);
                    sharesGdnums.add(sharesBgcz);
                }
                gdList.addAll(sharesGdnums);
                sharesGdnumService.insertBatch(sharesGdnums);
            }catch ( Exception e){
                e.printStackTrace();
            }
        }

        //更新公布时的收盘价
        sharesDetailService.updateCprice(gdList);

    }

    /**
     * 增减持数据
     */
    @Override
    public void getzcjc() {
        Wrapper<SharesDirection> wrdd=new EntityWrapper<>();
        sharesDirectionService.delete(wrdd);

        long st = System.currentTimeMillis();
        Wrapper<SharesDetail> wrd=new EntityWrapper<>();
        wrd.eq("status",88);
        List<SharesDetail> sharesDetails = sharesDetailService.selectList(wrd);

        for (SharesDetail sharesCode : sharesDetails) {
            String szorsh = sharesCode.getCode();
            System.out.println(szorsh);
            String name = sharesCode.getName();
            String substring = szorsh.substring(2);
            if(substring.startsWith("688")){
                continue;
            }
            String url="https://datacenter-web.eastmoney.com/api/data/v1/get" +
                    "?callback=jQuery11230849410572685454_"+new Date().getTime()+
                    "&sortColumns=END_DATE%2CSECURITY_CODE%2CEITIME" +
                    "&sortTypes=-1%2C-1%2C-1&pageSize=50&pageNumber=1" +
                    "&reportName=RPT_SHARE_HOLDER_INCREASE&quoteColumns=f2~01~SECURITY_CODE~NEWEST_PRICE%2Cf3~01~SECURITY_CODE~CHANGE_RATE_QUOTES" +
                    "&columns=ALL&source=WEB&client=WEB&filter=(SECURITY_CODE%3D%22" +
                    substring +
                    "%22)";

            try{
                HttpGet get=new HttpGet(url);
                CloseableHttpResponse execute = HttpClient5Utils.httpclient.execute(get);
                String result = EntityUtils.toString(execute.getEntity(), "UTF-8");
                int rIndex = result.indexOf("(");
                String resultStr = result.substring(rIndex + 1, result.length() - 2);
                JSONObject datas2 = JSONObject.parseObject(resultStr);
                String data = datas2.getString("result");
                String klines = JSONObject.parseObject(data).getString("data");
                List<Object> parse = (List<Object>) JSON.parse(klines);

                List<SharesDirection> notices=new ArrayList<>();
                for (Object o : parse) {
                    SharesDirection sharesDirection=new SharesDirection();
                    String DIRECTION = JSONObject.parseObject(o.toString()).getString("DIRECTION");
                    String START_DATE = JSONObject.parseObject(o.toString()).getString("START_DATE");
                    String TRADE_DATE = JSONObject.parseObject(o.toString()).getString("TRADE_DATE");
                    String NOTICE_DATE = JSONObject.parseObject(o.toString()).getString("NOTICE_DATE");
                    String HOLDER_NAME = JSONObject.parseObject(o.toString()).getString("HOLDER_NAME");
                    String CHANGE_NUM = JSONObject.parseObject(o.toString()).getString("CHANGE_NUM");
                    String CHANGE_FREE_RATIO = JSONObject.parseObject(o.toString()).getString("CHANGE_FREE_RATIO");
                    sharesDirection.setChangNum(CHANGE_NUM);
                    sharesDirection.setCode(szorsh);
                    sharesDirection.setStTime(START_DATE);
                    sharesDirection.setEnTime(TRADE_DATE);
                    sharesDirection.setNoTime(NOTICE_DATE);
                    sharesDirection.setGdName(HOLDER_NAME);
                    sharesDirection.setDirection(DIRECTION);
                    sharesDirection.setZb(CHANGE_FREE_RATIO);
                    notices.add(sharesDirection);
                }
                sharesDirectionService.insertBatch(notices);
            }catch (Exception e){
                System.out.println("err");
            }
        }
        long en = System.currentTimeMillis();
        long l = (en - st)/1000;
        System.out.println(l);
    }

    /**
     *
     */
    @Override
    public void getgggg() {

        //删除最近
        Wrapper<SharesNotice> wr=new EntityWrapper<>();
        sharesNoticeService.delete(wr);

        long st = System.currentTimeMillis();
        Wrapper<SharesDetail> wrd=new EntityWrapper<>();
        wrd.eq("status",88);
        List<SharesDetail> sharesDetails = sharesDetailService.selectList(wrd);

        for (SharesDetail sharesCode : sharesDetails) {
            String szorsh = sharesCode.getCode();
            String name = sharesCode.getName();
            String substring = szorsh.substring(2);
            if(substring.startsWith("688")){
                continue;
            }
            String  url="https://np-anotice-stock.eastmoney.com/api/security/ann" +
                    "?cb=jQuery112303162666297121548_"+new Date().getTime()+
                    "&sr=-1&page_size=50&page_index=1&ann_type=A&client_source=web" +
                    "&stock_list="+substring+"&f_node=0&s_node=0"  ;

            try{
                HttpGet get=new HttpGet(url);
                CloseableHttpResponse execute = HttpClient5Utils.httpclient.execute(get);
                String result = EntityUtils.toString(execute.getEntity(), "UTF-8");
                int rIndex = result.indexOf("(");
                String resultStr = result.substring(rIndex + 1, result.length() - 1);
                JSONObject datas2 = JSONObject.parseObject(resultStr);
                String data = datas2.getString("data");
                String klines = JSONObject.parseObject(data).getString("list");
                List<Object> parse = (List<Object>) JSON.parse(klines);

                List<SharesNotice> notices=new ArrayList<>();
                for (Object o : parse) {
                    SharesNotice sharesNotice=new SharesNotice();
                    String notice_date = JSONObject.parseObject(o.toString()).getString("notice_date");
                    String title_ch = JSONObject.parseObject(o.toString()).getString("title_ch");
                    String columns = JSONObject.parseObject(o.toString()).getString("columns");




                    if(!StringUtil.isEmpty(columns)){
                        List<Object> columnslist = (List<Object>) JSON.parse(columns);
                        Object o1 = columnslist.get(0);
                        String column_name= JSONObject.parseObject(o1.toString()).getString("column_name");
                        sharesNotice.setType(column_name);
                    }
                    sharesNotice.setCode(szorsh);
                    sharesNotice.setTitle(title_ch);
                    sharesNotice.setTime(notice_date);
                    //股东增减持
                    if(!"股东/实际控制人股份减持".equals(sharesNotice.getType())){
                        //7天内
                        if(DateUtil.compareDate(notice_date.substring(0,10),DateUtil.getYMDByDaysAgo(14))){
                            continue;
                        }
                    }
                    notices.add(sharesNotice);
                }
                if(!CollectionUtils.isEmpty(notices)){
                    sharesNoticeService.insertBatch(notices);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }



        long en = System.currentTimeMillis();
        long l = (en - st)/1000;
        System.out.println(l);
    }

    /**
     * 晚上更新耗时多的数据
     */
    @Override
    public void updateOthre() {
        //个股公告
        System.out.println("个股公告抓取");
        this.getgggg();
        //股东增减持
        System.out.println("股东增减持抓取");
        this.getzcjc();
        //股东人数
        System.out.println("股东人数抓取");
        this.getgdnum();
        //每天更新业绩预告
        sharesYjygService.getyjyg();
    }

    /**
     * 更新昨天股的数据
     */
    @Override
    public void getshares() {
            long st = System.currentTimeMillis();
            Wrapper<SharesDetail> wrd=new EntityWrapper<>();
            wrd.eq("status","88");
            List<SharesDetail> sharesDetails = sharesDetailService.selectList(wrd);

            List<CodeCjl> codeCjls=new ArrayList<>();
            for (SharesDetail sharesCode : sharesDetails) {
                String szorsh = sharesCode.getCode();
//                System.out.println(szorsh);
                String name = sharesCode.getName();
                String substring = szorsh.substring(2);
                String url="";
                if(szorsh.contains("sz")){
                    url="http://49.push2his.eastmoney.com/api/qt/stock/kline/get" +
                            "?cb=jQuery112408735255525730368_" + +new Date().getTime()+
                            "&secid=0." +substring +
                            "&ut=fa5fd1943c7b386f172d6893dbfba10b" +
                            "&fields1=f1%2Cf2%2Cf3%2Cf4%2Cf5%2Cf6" +
                            "&fields2=f51%2Cf52%2Cf53%2Cf54%2Cf55%2Cf56%2Cf57%2Cf58%2Cf59%2Cf60%2Cf61" +
                            "&klt=101&fqt=1&end=20500101&lmt=120&_="+new Date().getTime();
                }

                if(szorsh.contains("sh")){
                    url="http://49.push2his.eastmoney.com/api/qt/stock/kline/get" +
                            "?cb=jQuery112408735255525730368_" + +new Date().getTime()+
                            "&secid=1." +substring +
                            "&ut=fa5fd1943c7b386f172d6893dbfba10b" +
                            "&fields1=f1%2Cf2%2Cf3%2Cf4%2Cf5%2Cf6" +
                            "&fields2=f51%2Cf52%2Cf53%2Cf54%2Cf55%2Cf56%2Cf57%2Cf58%2Cf59%2Cf60%2Cf61" +
                            "&klt=101&fqt=1&end=20500101&lmt=120&_="+new Date().getTime();

                }

                try{
                    HttpGet get=new HttpGet(url);
                    CloseableHttpResponse execute = HttpClient5Utils.httpclient.execute(get);
                    String result = EntityUtils.toString(execute.getEntity(), "UTF-8");
                    int rIndex = result.indexOf("(");
                    String resultStr = result.substring(rIndex + 1, result.length() - 2);
                    JSONObject datas2 = JSONObject.parseObject(resultStr);
                    String data = datas2.getString("data");
                    String klines = JSONObject.parseObject(data).getString("klines");
                    List<Object> parse = (List<Object>) JSON.parse(klines);
                    CodeCjl codeCjl=codeCjlService.saveCjl(parse,szorsh,name);
                    int index=codeCjlService.nineTurn(parse,szorsh,name);//神奇九转

                    if(null!=codeCjl){
                        codeCjl.setIndex(index);
                        if("1".equals(sharesCode.getType())){
                            codeCjl.setType("1");
                            codeCjls.add(codeCjl);
                        }else{
                            codeCjls.add(codeCjl);
                        }

                    }else{
                        System.out.println(szorsh+"出错");
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            Wrapper<CodeCjl> wrdd=new EntityWrapper<>();
            wrdd.eq("time",codeCjls.get(0).getTime());
            codeCjlService.delete(wrdd);
            codeCjlService.insertBatch(codeCjls);
            long en = System.currentTimeMillis();
            long l = (en - st)/1000;
            System.out.println(l);
    }

    /**
     * 资金流向
     */
    @Override
    public void getzjlx() {
        //概念资金
        List<SharesZjlx> sharesZjlxes=new ArrayList<>();
        int gnint=1;
        for (int i=1;i<8;i++){
            try{
                String urlgn="https://push2.eastmoney.com/api/qt/clist/get?" +
                        "cb=jQuery112301368834420111369_"+new Date().getTime()+
                        "&fid=f62&po=1&pz=50&pn=" +
                        i +
                        "&np=1&fltt=2&invt=2" +
                        "&ut=b2884a393a59ad64002292a3e90d46a5&fs=m%3A90+t%3A3" +
                        "&fields=f12%2Cf14%2Cf2%2Cf3%2Cf62%2Cf184%2Cf66%2Cf69%2Cf72%2Cf75%2Cf78%2Cf81%2Cf84%2Cf87%2Cf204%2Cf205%2Cf124%2Cf1%2Cf13";

                HttpGet get=new HttpGet(urlgn);
                CloseableHttpResponse execute = HttpClient5Utils.httpclient.execute(get);
                String result = EntityUtils.toString(execute.getEntity(), "UTF-8");
                int rIndex = result.indexOf("(");
                String resultStr = result.substring(rIndex + 1, result.length() - 2);
                JSONObject datas2 = JSONObject.parseObject(resultStr);
                String data = datas2.getString("data");
                String klines = JSONObject.parseObject(data).getString("diff");
                List<SharesZjlx> sharesZjlxes1 = JSONObject.parseArray(klines, SharesZjlx.class);
                for (SharesZjlx sharesZjlx : sharesZjlxes1) {
                    sharesZjlx.setIndex(gnint++);
                    sharesZjlxes.add(sharesZjlx);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        //
        Map<String,SharesZjlx> GNZJ_MAP_BUFFER =new HashMap<>();
        MeUtils.mapCopy(SharesConstan.GNZJ_MAP, GNZJ_MAP_BUFFER);
        SharesConstan.GNZJ_MAP.clear();
        for (SharesZjlx sharesZjlx : sharesZjlxes) {
            String f14 = sharesZjlx.getF14();
            SharesZjlx vo = GNZJ_MAP_BUFFER.get(f14);
            if(null!=vo){
                List<Integer> indexList = vo.getIndexList();
                if(!CollectionUtils.isEmpty(indexList)){
                    indexList.add(sharesZjlx.getIndex());
                    sharesZjlx.setIndexList(indexList);
                }else{
                    List<Integer> voList=new ArrayList<>();
                    voList.add(sharesZjlx.getIndex());
                    sharesZjlx.setIndexList(voList);
                }
            }else{
                List<Integer> voList=new ArrayList<>();
                voList.add(sharesZjlx.getIndex());
                sharesZjlx.setIndexList(voList);
            }
            MeUtils.getMinVue(sharesZjlx);
            SharesConstan.GNZJ_MAP.put(f14,sharesZjlx);
        }

        //板块资金
        int bkint=1;
        List<SharesZjlx> sharesZjlxebks=new ArrayList<>();
        for (int i=1;i<3;i++){
            try{

                String urlbk="https://push2.eastmoney.com/api/qt/clist/get" +
                        "?cb=jQuery112301368834420111369_"+new Date().getTime()+
                        "&fid=f62&po=1&pz=50&pn=" +
                        i +
                        "&np=1&fltt=2&invt=2" +
                        "&ut=b2884a393a59ad64002292a3e90d46a5&fs=m%3A90+t%3A2" +
                        "&fields=f12%2Cf14%2Cf2%2Cf3%2Cf62%2Cf184%2Cf66%2Cf69%2Cf72%2Cf75%2Cf78%2Cf81%2Cf84%2Cf87%2Cf204%2Cf205%2Cf124%2Cf1%2Cf13";
                HttpGet get=new HttpGet(urlbk);
                CloseableHttpResponse execute = HttpClient5Utils.httpclient.execute(get);
                String result = EntityUtils.toString(execute.getEntity(), "UTF-8");
                int rIndex = result.indexOf("(");
                String resultStr = result.substring(rIndex + 1, result.length() - 2);
                JSONObject datas2 = JSONObject.parseObject(resultStr);
                String data = datas2.getString("data");
                String klines = JSONObject.parseObject(data).getString("diff");
                List<SharesZjlx> voList = JSONObject.parseArray(klines, SharesZjlx.class);
                for (SharesZjlx sharesZjlx : voList) {
                    sharesZjlx.setIndex(bkint++);
                    sharesZjlxebks.add(sharesZjlx);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        //资金流向排名变动
        Map<String,SharesZjlx> BKZJ_MAP_BUFFER = new HashMap<>();
        MeUtils.mapCopy(SharesConstan.BKZJ_MAP, BKZJ_MAP_BUFFER);
        SharesConstan.BKZJ_MAP.clear();
        for (SharesZjlx sharesZjlxebk : sharesZjlxebks) {
            String f14 = sharesZjlxebk.getF14();
            SharesZjlx sharesZjlx = BKZJ_MAP_BUFFER.get(sharesZjlxebk.getF14());
            if(null!=sharesZjlx){
                List<Integer> indexList = sharesZjlx.getIndexList();
                if(!CollectionUtils.isEmpty(indexList)){
                    indexList.add(sharesZjlxebk.getIndex());
                    sharesZjlxebk.setIndexList(indexList);
                }else{
                    List<Integer> voList=new ArrayList<>();
                    voList.add(sharesZjlxebk.getIndex());
                    sharesZjlxebk.setIndexList(voList);
                }
            }else{
                List<Integer> voList=new ArrayList<>();
                voList.add(sharesZjlxebk.getIndex());
                sharesZjlxebk.setIndexList(voList);
            }
            MeUtils.getMinVue(sharesZjlxebk);
            SharesConstan.BKZJ_MAP.put(f14,sharesZjlxebk);
        }

        //个股资金流向
        List<SharesZjlx> sharesZjlxeggs=new ArrayList<>();
        int ggint=1;
        for(int i=1;i<23;i++){
            try {
                String ggurl="https://push2.eastmoney.com/api/qt/clist/get" +
                        "?cb=jQuery112309718532307256402_"+new Date().getTime()+
                        "&fid=f62&po=1&pz=50&pn=" +
                        i +
                        "&np=1&fltt=2&invt=2&ut=b2884a393a59ad64002292a3e90d46a5" +
                        "&fs=m%3A0%2Bt%3A80%2Bf%3A!2" +
                        "&fields=f12%2Cf14%2Cf2%2Cf3%2Cf62%2Cf184%2Cf66%2Cf69%2Cf72%2Cf75%2Cf78%2Cf81%2Cf84%2Cf87%2Cf204%2Cf205%2Cf124%2Cf1%2Cf13";
                HttpGet get=new HttpGet(ggurl);
                CloseableHttpResponse execute = HttpClient5Utils.httpclient.execute(get);
                String result = EntityUtils.toString(execute.getEntity(), "UTF-8");
                int rIndex = result.indexOf("(");
                String resultStr = result.substring(rIndex + 1, result.length() - 2);
                JSONObject datas2 = JSONObject.parseObject(resultStr);
                String data = datas2.getString("data");
                String klines = JSONObject.parseObject(data).getString("diff");
                List<SharesZjlx> sharesZjlxes1 = JSONObject.parseArray(klines, SharesZjlx.class);
                for (SharesZjlx sharesZjlx : sharesZjlxes1) {
                    sharesZjlx.setIndex(ggint++);
                    sharesZjlxeggs.add(sharesZjlx);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        Map<String,SharesZjlx> GGZJ_MAP_BUFFER=new HashMap<>();
        MeUtils.mapCopy(SharesConstan.GGZJ_MAP, GGZJ_MAP_BUFFER);
        SharesConstan.GGZJ_MAP.clear();
        for (SharesZjlx sharesZjlxegg : sharesZjlxeggs) {
            String f12 = sharesZjlxegg.getF12();
            //储存流动变化
            SharesZjlx sharesZjlx = GGZJ_MAP_BUFFER.get(f12);
            if(null!=sharesZjlx){
                List<Integer> indexList = sharesZjlx.getIndexList();
                if(!CollectionUtils.isEmpty(indexList)){
                    indexList.add(sharesZjlxegg.getIndex());
                    sharesZjlxegg.setIndexList(indexList);
                }else{
                    List<Integer> voList=new ArrayList<>();
                    voList.add(sharesZjlxegg.getIndex());
                    sharesZjlxegg.setIndexList(voList);
                }
            }else{
                List<Integer> voList=new ArrayList<>();
                voList.add(sharesZjlxegg.getIndex());
                sharesZjlxegg.setIndexList(voList);
            }
            MeUtils.getMinVue(sharesZjlxegg);
            SharesConstan.GGZJ_MAP.put(f12,sharesZjlxegg);
        }
       

    }

    /**
     * 行业市盈率平均
     */
    @Override
    public void gethyssl() {
        for (SharesDetail sharesDetail : SharesConstan.CODE_LIST) {
            String code = sharesDetail.getCode();
            String substring = code.substring(2);
            String secid="";
            if(code.contains("sz")){
                secid="0."+substring;
            }else{
                secid="1."+substring;
            }
            try{
               String url="http://push2.eastmoney.com/api/qt/slist/get?spt=1&np=3" +
                       "&fltt=2&invt=2" +
                       "&fields=f9,f12,f13,f14,f20,f23,f37,f45,f49,f134,f135,f129,f1000,f2000,f3000" +
                       "&ut=bd1d9ddb04089700cf9c27f6f7426281&cb=jQuery1124009589982267392894_"+new Date().getTime()+
                       "&secid=" +
                        secid +
                       "&_="+new Date().getTime();

                //板块资金
                HttpGet get=new HttpGet(url);
                CloseableHttpResponse execute = HttpClient5Utils.httpclient.execute(get);
                String result = EntityUtils.toString(execute.getEntity(), "UTF-8");
                int rIndex = result.indexOf("(");
                String resultStr = result.substring(rIndex + 1, result.length() - 2);
                JSONObject datas2 = JSONObject.parseObject(resultStr);
                String data = datas2.getString("data");
                String klines = JSONObject.parseObject(data).getString("diff");
//                List<Object> parse = (List<Object>) JSON.parse(klines);
                List<SharesHyVsggVo> sharesHyVsggVos = JSONObject.parseArray(klines, SharesHyVsggVo.class);
                SharesHyVsggVo ggvo = sharesHyVsggVos.get(0);
                SharesConstan.GGSYL_MAP.put(code,ggvo);
                SharesHyVsggVo hyvo = sharesHyVsggVos.get(1);
                SharesConstan.HYSYL_MAP.put(hyvo.getF14(),hyvo);
            }catch (Exception e){
                System.out.println(code+"syl出错");
            }
        }


    }

    /**
     * 收盘后保存当天的数据
     */
    @Override
    public void saveAfter() {
        Wrapper<HintShares> wr=new EntityWrapper<>();
        wr.eq("time",DateUtil.getDate_yyyyMMdd());
        List<HintShares> hintSharesList = hintSharesService.selectList(wr);

        for (HintShares hintShares : hintSharesList) {
            String code = hintShares.getCode();
            HintShares vo = SharesConstan.INFORM_SHARES_MAP.get(code);
            hintShares.setKeepTotal(vo.getKeepTotal());
            hintShares.setKeepNum(vo.getKeepNum());
            hintShares.setNewZdf(vo.getNewZdf());//最后涨跌幅
        }
        hintSharesService.updateBatchById(hintSharesList);

    }
}
